<?php
class Comment extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{comment}}';
	}

	public function rules()
	{
		return array(
			array('uid, rela_id, rate', 'numerical', 'integerOnly'=>true),
			array('rela_type', 'length', 'max'=>10),
			array('content','length','min'=>8,'max'=>5000),
			array('rela_type,rela_id,content','required'),
			array('id, uid, rela_type, rela_id, rate, content, addtime', 'safe'),
		);
	}

	public function relations()
	{
		return array(
			'user'=>array(self::BELONGS_TO,'User','uid'),
			'topic'=>array(self::BELONGS_TO,'Topic','rela_id'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id'		=>	'ID',
			'uid'		=>	'UID',
			'rela_type'	=>	'贴子',
			'rela_id'	=>	'贴子ID',
			'rate'		=>	'满意度',
			'content'	=>	'评论内容',
			'addtime'	=>	'Addtime',
		);
	}

	public function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->addtime=new CDbExpression('now()');
				$this->rate=0;
			}
			$this->content=htmlentities($this->content);
			$this->content=nl2br($this->content);
		}
		return TRUE;
	}

}
?>
